﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using AspNetCs.Model;
using AspNetCs.Common;
using System.Data;

namespace AspNetCs.SQLServerDAL
{
    public class UserPhoto_DAL : IUserPhoto
    {
        #region IUserPhoto 成员
        public UserPhoto_Model GetModel(DataRow dr)
        {
            UserPhoto_Model md = new UserPhoto_Model();
            if (dr["ID"] != null) { md.Id = StringHelper.isNumber_int(dr["Id"].ToString()); }
            if (dr["ClassID"] != null) { md.ClassId = StringHelper.isNumber_int(dr["ClassID"].ToString()); }
            if (dr["photoName"] != null) { md.PhotoName = dr["photoName"].ToString(); }
            if (dr["CreateId"] != null) { md.CreateId = StringHelper.isNumber_int(dr["CreateId"].ToString()); }
            if (dr["hits"] != null) { md.Hits = StringHelper.isNumber_int(dr["hits"].ToString()); }
            if (dr["Addtime"] != null) { md.Addtime = StringHelper.isDatetime(dr["Addtime"].ToString()); }
            if (dr["photoUrl"] != null) { md.PhotoUrl = dr["photoUrl"].ToString(); }
            if (dr["Ischeck"] != null) { md.IsCheck = StringHelper.isNumber_int(dr["Ischeck"].ToString()); }
            if (dr["Readme"] != null) { md.Readme = dr["Readme"].ToString(); }
            if (dr["isdel"] != null) { md.IsDel = StringHelper.isNumber_int(dr["isdel"].ToString()); }
            if (dr["Types"] != null) { md.Types = StringHelper.isNumber_int(dr["Types"].ToString()); }
            if (dr["ChannelID"] != null) { md.ChannelID = StringHelper.isNumber_int(dr["ChannelID"].ToString()); }
            return md;
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="md"></param>
        /// <returns></returns>
        public int Add(UserPhoto_Model md)
        {
            int result = -1;
            db.ParameterArry[] par = new db.ParameterArry[8];
            par[0].objValue = md.ClassId;
            par[0].parName = "@ClassID";
            par[0].dbType = SqlDbType.Int;

            par[1].objValue = md.PhotoName;
            par[1].parName = "@photoName";
            par[1].dbType = SqlDbType.VarChar;

            par[2].objValue = md.CreateId;
            par[2].parName = "@CreateId";
            par[2].dbType = SqlDbType.Int;


            par[3].objValue = md.PhotoUrl;
            par[3].parName = "@photoUrl";
            par[3].dbType = SqlDbType.VarChar;


            par[4].objValue = md.IsCheck;
            par[4].parName = "@Ischeck";
            par[4].dbType = SqlDbType.Int;


            par[5].objValue = md.Readme;
            par[5].parName = "@Readme";
            par[5].dbType = SqlDbType.Text;



            par[6].objValue = md.Types;
            par[6].parName = "@Types";
            par[6].dbType = SqlDbType.Int;


            par[7].objValue = md.ChannelID;
            par[7].parName = "@ChannelID";
            par[7].dbType = SqlDbType.Int;
            db Conn = new db();
            result = Conn.ExcSql("Insert into UserPhoto(ClassID,photoName,CreateId,photoUrl,Ischeck,Readme,Types,ChannelID) values(@ClassID,@photoName,@CreateId,@photoUrl,@Ischeck,@Readme,@Types,@ChannelID)", par);
            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public int delete(string filter)
        {
            int result = -1;
            db Conn = new db();
            if (filter != "")
            {
                filter = " where " + filter;
            }

            string sql = "delete from UserPhoto  " + filter;
            result = Conn.ExcSql(sql);
            return result;
        }

        public DataTable GetDataTable(string filter)
        {
            db Conn = new db();
            if (filter != "")
            {
                filter = " where " + filter;
            }

            return Conn.GetSql_DataTable("Select * from  UserPhoto " + filter);

        }

        public int UpdateCheck(UserPhoto_Model md)
        {
            int result = 0;
            db Conn = new db();
            try
            {
                result = Conn.ExcSql("Update UserPhoto Set Ischeck=" + md.IsCheck + " where id=" + md.Id.ToString());
            }
            catch
            {
                result = -1;
            }
            finally
            {
                Conn.close();
            }
            return result;
        }

        public int Update(UserPhoto_Model md)
        {

            int result = -1;
            db.ParameterArry[] par = new db.ParameterArry[6];
            par[0].objValue = md.ClassId;
            par[0].parName = "@ClassID";
            par[0].dbType = SqlDbType.Int;

            par[1].objValue = md.PhotoName;
            par[1].parName = "@photoName";
            par[1].dbType = SqlDbType.VarChar;

            par[2].objValue = md.Id;
            par[2].parName = "@ID";
            par[2].dbType = SqlDbType.Int;

            par[3].objValue = md.PhotoUrl;
            par[3].parName = "@photoUrl";
            par[3].dbType = SqlDbType.VarChar;


            par[4].objValue = md.IsCheck;
            par[4].parName = "@Ischeck";
            par[4].dbType = SqlDbType.Int;

            par[5].objValue = md.Readme;
            par[5].parName = "@Readme";
            par[5].dbType = SqlDbType.Text;

            db Conn = new db();
            result = Conn.ExcSql("Update UserPhoto Set ClassID=@ClassID,photoName=@photoName,photoUrl=@photoUrl,Ischeck=@Ischeck,Readme=@Readme where id=@ID ", par);
            return result;
        }

        #endregion
    }
}
